<?php
namespace app\admin\controller\account;

use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupRule;
use app\admin\model\AuthRule;
use think\Db;

use app\common\controller\SysCommon;

class Group extends SysCommon
{
    protected $model = '';

    public function initialize()
    {
        parent::initialize();
        $this->model = new AuthGroup();

    }
    public function index()
    {
        if(request()->isPost()){
            //用户表的实例化
            $post = input('post.');
            // 姓名 查询
            if(!empty($post['key']['title'])){
                $where[] = ['title','like','%'.$post['key']['title'].'%'];
            }
            $where[] = ['id','gt','0'];
            $count   = $this->model->getCount($where);
            $where['page'] = $post['page'];
            $res   = $this->model->wechatList($where,"*","id desc",$post['limit']);

            return ajax_return('0','角色列表',$count,$res);

        }else{
            $auth_rule_model = new AuthRule();
            $auth = $auth_rule_model->getLevelData();
            $auth=array_values($auth);
//            dump($auth);
            $this->assign('auth', $auth);
            return $this->fetch();
        }
    }
    //获取当前角色的 所有权限值
    public function get_auth(){
        if(request()->isPost()){
            $post = input('post.');
            $groupRuleModel = new AuthGroupRule();
            $ids = $groupRuleModel->getIds($post['id']);
            return ajax_return('1','角色所拥有的id','',$ids);
        }
    }
    //修改 权限
    public function auth(){
        $groupRuleModel = new AuthGroupRule();
        if(request()->isPost()){

            $post = input('post.');
//            die();
            //如果有权限
            if(count($post['auth'])>0){
                $list = [];
                foreach($post['auth'] as $value){
                    array_push($list,$value['value']);
                }
                $res = $groupRuleModel->saveData($post['id'],$list);
                if($res){
                    return ajax_return('1','操作成功','',[]);
                }else{
                    return ajax_return('0','操作失败','',[]);
                }
            }
            $ids = $groupRuleModel->getIds($post['id']);
            return ajax_return('1','角色所拥有的id','',$ids);
        }
    }


}